class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 如果目标值存在返回下标，否则返回 -1
     * @param nums int整型vector 
     * @param target int整型 
     * @return int整型
     */
    int search(vector<int>& nums, int target) {
        int len = nums.size();
        int l = 0;
        int r = len - 1;
        int mid;
        int index = -1;
        while(l <= r) {
            mid = l + (r-l)/2;
            if(nums[mid] ==  target)
            {
                r = mid-1;
                index = mid;
            }
            else if(nums[mid] < target)
                l = mid + 1;
            else if(nums[mid] > target)
                r = mid-1;
        }
        return index;
    }
    /*int search(vector<int>& nums, int target) {
        int len = nums.size();
        int l = 0;
        int r = len;
        int mid;
        int index = -1;
        while(l < r) {
            mid = l + (r-l)/2;
            if(nums[mid] ==  target)
            {
                r = mid;
                index = mid;
            }
            else if(nums[mid] < target)
                l = mid + 1;
            else if(nums[mid] > target)
                r = mid;
        }
        return index;
    }*/
};